﻿var ServiceId = GetQueryString("Id");
var dataSourceId = GetQueryString("dataSourceId");
var tableId = GetQueryString("tableId");
var sysno = GetQueryString("sysno");
var columnCode;
var columnData;
var edit;
$(function () {
    var obj = document.getElementById('add-parameter-modal-container');
    rDrag.init(obj);
    getServiceDetails();
    getDataSourceDetails();
    GetDataTableInfo();
    $("#ServiceId").val(ServiceId);
    $("#SourceId").val(dataSourceId);
    $("#TableId").val(tableId);
    $("#Chose-Form").submit(function () {
        var columns = $("#dataBaseColumn tbody tr");
        var columnstr = "";
        var logocolumns = $(".LogoColumn:checked");
        var logocolumnstr = "";
        if (columns.length > 0) {
            for (var i = 0; i < columns.length; i++) {
                var tempjson = JSON.parse($(columns[i]).attr("data"));
                if (tempjson.column.Id != undefined) {
                    if (columnstr == "") {
                        columnstr += (tempjson.column.Id + ",");
                    } else {
                        columnstr += ("," + tempjson.column.Id + ",");
                    }
                    columnstr += (tempjson.database.Id);
                } else {
                    continue;
                }
            }
            for (var j = 0; j < logocolumns.length; j++) {
                var tempobj = $(logocolumns[j]);
                logocolumnstr += tempobj.val();
                if (j < (logocolumns.length - 1)) {
                    logocolumnstr += ",";
                }
            }
            $("#DataIdentity").val(logocolumnstr);
            $("#ColumnMapping").val(columnstr);
            var url = "DataServiceManagement.html";
            $.ajax({
                url: apiUrl,
                type: "Post",
                dataType: "json",
                data: $(this).serialize(),
                success: function (data) {
                    if (data.IsSccuess) {
                        ShowGritter("保存成功!", 2, url);
                    } else {
                        if (IsLogined(data.ErrorCode)) {
                            alert("失败（" + "错误代码：" + data.ErrorCode + "，错误信息：" + data.Message + ")");
                        }
                    }
                },
                beforeSend: function () {
                    ShowGritter("正在提交");
                }, error: function () {
                    alert("系统异常请稍后尝试！");
                }
            });

        } else {
            alert("暂无可提交的列，请修改数据源后在关联数据表！");
        }
        return false;
    });
});

//获取服务的详细信息
var getServiceDetails = function () {
    $.ajax({
        url: apiUrl,
        data: {
            "sys:sno": sysno,
            "sys:act": "GetDataService",
            "ServiceId": ServiceId
        },
        dataType: "json",
        success: function (data) {
            if (data.IsSccuess) {
                $("#ServiceName").val(data.Data.Name);
            } else {
                if (IsLogined(data.ErrorCode)) {
                    alert("失败（" + "错误代码：" + data.ErrorCode + "，错误信息：" + data.Message + ")");
                }
            }
        },
        beforeSend: function () {
            // $("btn_login").html("登录中...");
        }
    });
}
var getDataSourceDetails = function () {
    $.ajax({
        url: apiUrl,
        data: {
            "sys:sno": "ETL",
            "sys:act": "GetDataSource",
            "DataServiceId": ServiceId,
            "DataSourceId": dataSourceId
        },
        dataType: "json",
        success: function (data) {
            if (data.IsSccuess) {
                $("#SourceName").val(data.Data.Name);//名称
                columnCode = data.Data.Columns;
                $("#alert-table").append(AssembleColumnData(columnCode));
                //AppadColumnData(data.Data.Columns);//组装数据并并填充表格
            } else {
                if (IsLogined(data.ErrorCode)) {
                    alert("失败（" + "错误代码：" + data.ErrorCode + "，错误信息：" + data.Message + ")");
                }
            }
        },
        beforeSend: function () {
            // $("btn_login").html("登录中...");
        }
    });
}
var GetDataTableInfo = function () {
    $.ajax({
        url: apiUrl,
        data: {
            "sys:sno": "ETL",
            "sys:act": "GetDataTable",
            "DataTableId": tableId,
        },
        dataType: "json",
        success: function (data) {
            if (data.IsSccuess) {
                $("#TableName").val(data.Data.Name);
                $("#IsRawDt").val();
                if (data.Data.IsRawDt) {
                    $("#IsRawDt").attr("checked", "checked");
                }
            } else {
                if (IsLogined(data.ErrorCode)) {
                    alert("失败（" + "错误代码：" + data.ErrorCode + "，错误信息：" + data.Message + ")");
                }
            }
        }
    });
    $.ajax({
        url: apiUrl,
        data: {
            "sys:sno": "DataTable",
            "sys:act": "GetColumns",
            "DataTableId": tableId,
        },
        dataType: "json",
        success: function (data) {
            if (data.IsSccuess) {
                columnData = data.Data.Columns;
                //$("#logocolumn").append(AssembleLogoData(columnData));
                $("#dataBaseColumn").append(AssembleData(columnData));
            } else {
                if (IsLogined(data.ErrorCode)) {
                    alert("失败（" + "错误代码：" + data.ErrorCode + "，错误信息：" + data.Message + ")");
                }
            }
        }
    });
}
//传入data对象返回一行的表格数据
var AssembleLogoData = function (data) {
    var tr = "";
    for (var i = 0; i < data.length; i++) {
        tr += ("<tr id=\"" + data[i].Id + "\">");
        tr += ("<td><label class=\"checkbox\"><input type=\"checkbox\" class=\"LogoColumn\" value=\"" + data[i].Id + "\"  /> <i></i></label></td>");
        tr += ("<td>" + data[i].Name + "</td></tr>");
    }
    return tr;
}
var AddLogoData = function (data) {
    var tr = "";
    tr += ("<tr id=\"" + data.column.Id + "\">");
    tr += ("<td><label class=\"checkbox\"><input type=\"checkbox\" class=\"LogoColumn\" value=\"" + data.column.Id + "\"  /> <i></i></label></td>");
    tr += ("<td>" + data.column.Name + "</td></tr>");
    return tr;
}
//传入data对象返回一行的表格数据
var AssembleData = function (data) {
    var tr = "";
    for (var i = 0; i < data.length; i++) {
        tr += ("<tr data='" + JSON.stringify({ database: data[i], column: {} }) + "'>");
        tr += "<td><a href='javascript:void(0);' onclick='selectDataFild(this)' data='" + data[i].Id + "' >" + "选择列" + "</a></td>";
        tr += "<td>" + data[i].Name + "</td>";
        tr += "<td>" + data[i].Type + "</td>";
        tr += "<td>" + data[i].Length + "</td>";
        tr += ("</tr>");
    }
    return tr;
}
//传入data对象返回一行的表格数据
var AssembleColumnData = function (data) {
    var tr = "";
    for (var i = 0; i < data.length; i++) {
        tr += ("<tr>");
        tr += ("<td>" + data[i].Name + "</td>");
        tr += ("<td>" + data[i].Type + "</td>");
        tr += ("<td><a href=\"javascript:void(0);\" data='" + JSON.stringify(data[i]) + "'onclick=\"selectThisRow(this)\">选择</a>" + "</td></tr>");
    }
    return tr;
}
var AddColumnData = function (data) {
    var tr = "";
    tr += ("<tr>");
    tr += ("<td>" + data.Name + "</td>");
    tr += ("<td>" + data.Type + "</td>");
    tr += ("<td><a href=\"javascript:void(0);\" data='" + JSON.stringify(data) + "'onclick=\"selectThisRow(this)\">选择</a>" + "</td></tr>");
    return tr;
}
var selectDataFild = function (obj) {
    var $this = $(obj);
    edit = $this.parent().parent();
    $("#add-parameter-modal").modal("show");
}